package TableHelpers;

import SQLAPI.SQL;
import SQLAPI.TransactionReport;
import SQLAPI.UsageReport;
import java.util.ArrayList;

/**
 *A TableHelper class that is used solely to build an HTML table from an ArrayList
 * of WebUser objects. 
 * @author Scott Catsirelis - 602705
 */
public class WebUserReports {
    
    private final SQL SQL = new SQL();
    
    /**
     * Returns a String which is a built HTML table with usage report information.
     * @param userid - int - userID to fetch usage reports for.
     * @return String - A String object that contains a built HTML table.
     */
    public String createUsageReportTable(int userid)
    {
        String table="<thead><tr>";
        table += "<th>Report ID</th>";
        table += "<th>User ID</th>";
        table += "<th>Time Logged</th>";
        table += "<th>Message</th>";
        table += "</tr></thead>";
        table += "<tbody style=\"overflow:auto\">";
        
        ArrayList<UsageReport> list = SQL.getUsageReports(userid);
        for(int i=0;i<list.size();i++)
        {
            UsageReport e = list.get(i);
            table+="<tr>";
            table+="<td>"+e.getUsageid()+"</td>";
            table+="<td>"+e.getUserid()+"</td>";
            table+="<td>"+e.getTime()+"</td>";
            table+="<td>"+e.getMessage()+"</td>";
            table += "</tr>";
        }
        table += "</tbody>";
        return table;
    }
    /**
     * Returns a String object that contains the text to build an HTML table
     * containing Transaction data.
     * @param userid - int - UserID to retrieve a Table with transaction data for.
     * @return 
     */
    public String createTransactionReportTable(int userid)
    {
        String table="<thead><tr>";
        table += "<th>Transaction ID</th>";
        table += "<th>Usage ID</th>";
        table += "<th>User ID</th>";
        table += "<th>Change Time</th>";
        table += "<th>Object Name</th>";
        table += "<th>Transaction Type</th>";
        table += "</tr></thead>";
        table += "<tbody style=\"overflow:auto\">";
        ArrayList<TransactionReport> list = SQL.getTransactionReports(userid);
        for(int i=0;i<list.size();i++)
        {
            TransactionReport e = list.get(i);
            table+="<tr>";
            table+="<td>"+e.getTransid()+"</td>";
            table+="<td>"+e.getUsageid()+"</td>";
            table+="<td>"+e.getUserid()+"</td>";
            table+="<td>"+e.getTime()+"</td>";
            table+="<td>"+e.getObjName()+"</td>";
            if(e.getType().equals("MODIFY"))
            {
                table+="<td><a href=\"transactionDetails.jsp?transid="+e.getTransid()+"\">MODIFY</a></td>";
            }
            else{
            table+="<td>"+e.getType()+"</td>";}
            table+="</tr>";
        }
        table += "</tbody>";
        return table;
    }
}
